package cn.edu.nuc.chat.frame;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import cn.edu.nuc.chat.client.TCPClient;
import cn.edu.nuc.chat.model.InitInfo;
import cn.edu.nuc.chat.model.User;
import cn.edu.nuc.chat.util.Token;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JToggleButton;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JTextField txtPort;
	private JTextField txtIP;
	private JTextField txtUUID;
	private JList users;
	private JLabel lblUser;
	private DefaultListModel<User> model;
	private JTextField txtNickName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 557, 471);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtPort = new JTextField();
		txtPort.setText("8000");
		txtPort.setBounds(168, 11, 66, 21);
		contentPane.add(txtPort);
		txtPort.setColumns(10);
		
		model = new DefaultListModel<User>();
		users = new JList(model);
		users.setBounds(10, 39, 104, 384);
		users.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				JList<User> jl = (JList)e.getSource();
				User u = jl.getSelectedValue();
				lblUser.setText(u.getNickName());
				txtUUID.setText(u.getUuid());
				
				TCPClient.TO_UUID = u.getUuid();
			}
		});
		contentPane.add(users);
		
		JTextArea textMessage = new JTextArea();
		textMessage.setBounds(124, 81, 406, 215);
		contentPane.add(textMessage);
		
		JTextArea textSend = new JTextArea();
		textSend.setBounds(124, 327, 406, 63);
		contentPane.add(textSend);
		
		JButton btnNewButton = new JButton("发送");
		btnNewButton.setEnabled(false);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread( new Runnable() {
					
					@Override
					public void run() {
						StringBuilder msg = new StringBuilder();
						msg.append("msg");
						msg.append( Token.DELIM );
						msg.append( TCPClient.TO_UUID );	//收消息方的UUID
						msg.append( Token.DELIM );
						msg.append( TCPClient.MY_UUID );	//自己的UUID
						msg.append( Token.DELIM );
						msg.append( txtNickName.getText());	//昵称
						msg.append( Token.DELIM );
						msg.append( textSend.getText() );	//消息内容
						
						if( TCPClient.send( msg.toString() ) ){
							textSend.setText("");
						} else {
							//发送失败提示
						}
					}
				}).start();
			}
		});
		btnNewButton.setBounds(438, 400, 93, 23);
		contentPane.add(btnNewButton);
		
		lblUser = new JLabel();
		lblUser.setBounds(124, 40, 54, 15);
		contentPane.add(lblUser);
		
		JLabel lblip = new JLabel("服务器IP:");
		lblip.setBounds(10, 14, 54, 15);
		contentPane.add(lblip);
		
		txtIP = new JTextField();
		txtIP.setText("127.0.0.1");
		txtIP.setBounds(74, 11, 74, 21);
		contentPane.add(txtIP);
		txtIP.setColumns(10);
		
		JToggleButton btnStart = new JToggleButton("连接");
		btnStart.setBounds(244, 10, 95, 23);
		
		btnStart.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JToggleButton btn = (JToggleButton)e.getSource();
				if( ! btn.isSelected() ){
					TCPClient.stop();
					btn.setText("连接");
					btnNewButton.setEnabled(true);
				} else {
					InitInfo info = new InitInfo();
					info.setHost( txtIP.getText() );
					info.setPort(Integer.parseInt(txtPort.getText().trim()));
					info.setUser(new User(TCPClient.MY_UUID, txtNickName.getText()));
					
					//如果连接成功
					if(TCPClient.start(info, textMessage, users)){
						btnNewButton.setEnabled(true);
						btn.setText("关闭");
					}
				}
			}
		});
		contentPane.add(btnStart);
		
		txtUUID = new JTextField();
		txtUUID.setBounds(188, 37, 342, 21);
		txtUUID.setVisible(false);
		contentPane.add(txtUUID);
		txtUUID.setColumns(10);
		
		txtNickName = new JTextField();
		txtNickName.setText("张三");
		txtNickName.setBounds(349, 11, 135, 21);
		contentPane.add(txtNickName);
		txtNickName.setColumns(10);
	}
}
